package me.firstapp.api.shop;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.DateUtils;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.bbs.Member;
import me.firstapp.module.bbs.MemberPointAccountTrace;
import me.firstapp.module.bbs.PointAccount;
import me.firstapp.service.bbs.MemberPointAccountTraceService;
import me.firstapp.service.bbs.MemberService;
import me.firstapp.service.bbs.PointAccountService;

@Controller
@RequestMapping(value = "/point")
@Api(value = "/point", description = "积分相关接口")
public class PointApi extends BaseApi {

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Autowired
	private PointAccountService pointAccountService;

	@Autowired
	private MemberService memberService;

	@Autowired
	private MemberPointAccountTraceService memberPointAccountTraceService;

	@RequestMapping(value = "findMemberPoint.do", method = RequestMethod.GET)
	@ApiOperation(value = "获取用户积分", notes = "获取用户积分")
	public void executeFindMemberPoint(Long memberId, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> resultJsonObject = new SingleObject<Object>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			PointAccount pointAccount = pointAccountService.findByMemberId(memberId, apiClientId);
			resultJsonObject.setObject(pointAccount);
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			resultJsonObject.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, resultJsonObject);
	}

	@RequestMapping(value = "alterPayPassword.do", method = RequestMethod.POST)
	@ApiOperation(value = "修改支付密码", notes = "修改支付密码")
	public void alterPayPassword(Long memberId, String oldPassword, String newPassword, String code,
			HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			if (StrUtils.isNULL(code)) {
				throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "验证码");
			}
			Member member = memberService.findMemberById(memberId, apiClientId);
			if (member != null) {
				verifyCode(stringRedisTemplate, code, member.getEmail());
			}
			pointAccountService.updatePayPassword(memberId, oldPassword, newPassword, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findPointAccountTrace.do", method = RequestMethod.GET)
	@ApiOperation(value = "查询积分变动记录", notes = "查询积分变动记录")
	public void findPointAccountTrace(
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, Long memberId,
			HttpServletRequest request, HttpServletResponse response) {

		PageObject<MemberPointAccountTrace> result = new PageObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<MemberPointAccountTrace> tracePage = memberPointAccountTraceService
					.findMemberPointAccountTracePage(pageNo, pageSize, memberId, apiClientId);
			JsonPage<MemberPointAccountTrace> jsonPage = new JsonPage<MemberPointAccountTrace>();
			jsonPage.setList(tracePage.getEntities());
			jsonPage.setPageNo(tracePage.getPageNo());
			jsonPage.setPageSize(tracePage.getPageSize());
			jsonPage.setTotalCount(tracePage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findPointTracePageByParams.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询积分流水", notes = "分页查询积分流水")
	public void findPointTracePageByParams(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, Long startTime, Long endTime, Long memberId, HttpServletRequest request,
			HttpServletResponse response) {
		PageObject<MemberPointAccountTrace> result = new PageObject<MemberPointAccountTrace>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Date startDate = null;
			if (startTime != null) {
				startDate = DateUtils.getDateFromTimeStamp(String.valueOf(startTime), "yyyy-MM-dd");
			}
			Date endDate = null;
			if (endTime != null) {
				endDate = DateUtils.getDateFromTimeStamp(String.valueOf(endTime), "yyyy-MM-dd");
			}

			Page<MemberPointAccountTrace> tracePage = memberPointAccountTraceService.findPointAccountTracePageByParams(
					memberId, pageNo, pageSize, searchParams, sortParam, sortType, startDate, endDate, apiClientId);
			JsonPage<MemberPointAccountTrace> jsonPage = new JsonPage<MemberPointAccountTrace>();
			jsonPage.setList(tracePage.getEntities());
			jsonPage.setPageNo(tracePage.getPageNo());
			jsonPage.setPageSize(tracePage.getPageSize());
			jsonPage.setTotalCount(tracePage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/rechargePoint.do", method = RequestMethod.POST)
	@ApiOperation(value = "积分充值", notes = "积分充值")
	public void rechargePoint(Long memberId, String cardNo, String password, String payPassword, String code,
			HttpServletRequest request, HttpServletResponse response) {
		SingleObject<PointAccount> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Member member = memberService.findMemberById(memberId, apiClientId);
			if (member != null) {
				verifyCode(stringRedisTemplate, code, member.getEmail());
			}
			PointAccount pointAccount = pointAccountService.executeRecharge(memberId, cardNo, password, payPassword,
					apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(pointAccount);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
}
